package com.example.exceltosqlite.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.exceltosqlite.entity.SqliteFileEntity;
import com.example.exceltosqlite.entity.SqliteFileNumberEntity;
import com.example.exceltosqlite.entity.SqliteFileTypeEntity;
import com.example.exceltosqlite.sqlite.SQLliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by ColorRain on 2021/5/20.
 */

public class ExcelFileTypeDao {

    private Dao<SqliteFileTypeEntity, Integer> thisDao;
    private Context context;
    private SQLliteOpenHelper dbHelper;

    public ExcelFileTypeDao(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        dbHelper = SQLliteOpenHelper.getHelper(context);
        try {
            thisDao = dbHelper.getDao(SqliteFileTypeEntity.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询出所有的数据
     *
     * @return
     */
    public List<SqliteFileTypeEntity> queryAll() throws SQLException {
        List<SqliteFileTypeEntity> list = thisDao.queryForAll();
        return list;
    }


    /**
     * 查询出主键key
     *
     */
    public int queryKey(String type) throws SQLException {
        List<SqliteFileTypeEntity> tempList = thisDao.queryForEq("type", type);
        if(null == tempList || 0 == tempList.size()){
            return -1;
        }
        return tempList.get(0).getId();
    }


    /**
     * 插入
     *
     * @param sqliteFileTypeEntity
     * @return
     * @throws SQLException
     */
    public void insert(SqliteFileTypeEntity sqliteFileTypeEntity) throws SQLException {
        thisDao.create(sqliteFileTypeEntity);
    }

    /**
     * 插入
     *
     * @param sqliteFileEntityList
     * @return
     * @throws SQLException
     */
    public void insertAll(List<SqliteFileTypeEntity> sqliteFileEntityList) throws SQLException {
        thisDao.create(sqliteFileEntityList);
    }


    /**
     * 更新
     *
     * @param sqliteFileTypeEntity
     * @return
     * @throws SQLException
     */
    public void update(SqliteFileTypeEntity sqliteFileTypeEntity) throws SQLException {
        if (0 == queryAll().size()) {
            thisDao.create(sqliteFileTypeEntity);
        } else {
            thisDao.update(sqliteFileTypeEntity);
        }
    }


    //删除表
    public void deleteSql() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("DELETE FROM telegramType");
//        db.close();
    }


    /**
     * 删除一堆
     *
     * @param lst
     * @return
     * @throws SQLException
     */


    public int deleteList(List<SqliteFileTypeEntity> lst) throws SQLException {
        return thisDao.delete(lst);
    }


    /**
     * 删除指定字符串的行
     *
     * @param idName
     * @param idValue
     * @return
     * @throws SQLException
     */

    public int deleteById(String idName, String idValue) throws SQLException {
        SqliteFileTypeEntity t = queryById(idName, idValue);
        if (null != t) {
            return delete(t);
        }
        return 0;
    }

    public int delete(SqliteFileTypeEntity t) throws SQLException {
        return thisDao.delete(t);
    }


    public SqliteFileTypeEntity queryById(String idName, String idValue) throws SQLException {
        List<SqliteFileTypeEntity> lst = query(idName, idValue);
        if (null != lst && !lst.isEmpty()) {
            return lst.get(0);
        } else {
            return null;
        }
    }

    public List<SqliteFileTypeEntity> query(String attributeName, String attributeValue) throws SQLException {
        QueryBuilder<SqliteFileTypeEntity, Integer> queryBuilder = thisDao.queryBuilder();
        queryBuilder.where().eq(attributeName, attributeValue);
        PreparedQuery<SqliteFileTypeEntity> preparedQuery = queryBuilder.prepare();
        return query(preparedQuery);
    }

    public List<SqliteFileTypeEntity> query(PreparedQuery<SqliteFileTypeEntity> preparedQuery) throws SQLException {
        return thisDao.query(preparedQuery);
    }
}
